﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class MasterSearch : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(Telerik.Web.UI.GridDataItem row);
        public event AcceptEvent OnAcceptEvent;


        public string GetMasterUsername()
        {
            return txtMasterUsername.Text;
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnMasterSearch_Click(object sender, EventArgs e)
        {
            GPSTrackingDBV2.Data.MastersQuery MastersQuery = new GPSTrackingDBV2.Data.MastersQuery();
            if (!string.IsNullOrEmpty(txtSearchMasterUsername.Text)) MastersQuery.AppendContains(GPSTrackingDBV2.Entities.MastersColumn.MasterUsername, txtSearchMasterUsername.Text);
            if (!string.IsNullOrEmpty(txtSearchMasterName.Text)) MastersQuery.AppendContains(GPSTrackingDBV2.Entities.MastersColumn.Name, txtSearchMasterName.Text);
            if (!string.IsNullOrEmpty(txtSearchMasterFirstName.Text)) MastersQuery.AppendContains(GPSTrackingDBV2.Entities.MastersColumn.FirstName, txtSearchMasterFirstName.Text);
            if (!string.IsNullOrEmpty(txtSearchMasterLastName.Text)) MastersQuery.AppendContains(GPSTrackingDBV2.Entities.MastersColumn.LastName, txtSearchMasterLastName.Text);

            MasterGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.MastersProvider.Find(MastersQuery);
            MasterGrid.DataBind();
        }

        protected void btnMasterOK_Click(object sender, EventArgs e)
        {
            if (MasterGrid.SelectedItems.Count > 0)
            {
                txtMasterUsername.Text = MasterGrid.SelectedItems[0].Cells[3].Text;
                txtMasterName.Text = MasterGrid.SelectedItems[0].Cells[4].Text;
                txtMasterFirstName.Text = MasterGrid.SelectedItems[0].Cells[5].Text;
                txtMasterLastName.Text = MasterGrid.SelectedItems[0].Cells[6].Text;

                Telerik.Web.UI.GridDataItem row = MasterGrid.SelectedItems[0] as Telerik.Web.UI.GridDataItem;

                if (OnAcceptEvent != null) OnAcceptEvent(row);

            }
        }       
    }
}